<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search">
				<el-form :inline="true" size="default" :model="tableData.param" ref="queryRef">
					<el-form-item label="角色名称" prop="roleName">
						<el-input v-model="tableData.param.roleName" placeholder="请输入角色名称" clearable style="width: 180px" />
					</el-form-item>
					<el-form-item label="状态" prop="roleStatus">
						<el-select placeholder="请选择状态" v-model="tableData.param.roleStatus" clearable>
							<el-option label="启用" value="1" />
							<el-option label="禁用" value="0" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="roleList">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" @click="resetQuery(queryRef)">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重置
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAddRole" v-auth="'api/v1/system/role/add'">
							<el-icon>
								<ele-FolderAdd />
							</el-icon>
							新增角色
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%" stripe size="default">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="listOrder" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column label="更新时间" align="center" prop="updateTime" width="180" :formatter="formatter" />
				<el-table-column label="创建时间" align="center" prop="createTime" width="180" :formatter="formatter" />
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button
							size="small"
							text
							type="primary"
							@click="onOpenEditRole(scope.row)"
							v-auth-all="['api/v1/system/role/get', 'api/v1/system/role/edit']"
							v-if="scope.row.id !== 1"
							>修改
						</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-auth="'api/v1/system/role/delete'" v-if="scope.row.id !== 1"
							>删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination
				v-show="tableData.total > 0"
				:total="tableData.total"
				v-model:page="tableData.param.pageNum"
				v-model:limit="tableData.param.pageSize"
				@pagination="roleList"
			/>
		</el-card>
		<EditRole ref="editRoleRef" @getRoleList="roleList" />
	</div>
</template>

<script lang="ts">
import { defineComponent, getCurrentInstance, onMounted, reactive, ref, toRaw, toRefs } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import EditRole from '/@/views/system/role/component/editRole.vue';
import { deleteRole, getRoleList } from '/@/api/system/role';

// 定义接口来定义对象的类型
interface TableData {
	id: number;
	status: number;
	listOrder: number;
	name: string;
	remark: string;
	dataScope: number;
	updateTime: string;
	createTime: string;
}

interface TableDataState {
	tableData: {
		data: Array<TableData>;
		total: number;
		loading: boolean;
		param: {
			roleName: string;
			roleStatus: string;
			pageNum: number;
			pageSize: number;
		};
	};
}

export default defineComponent({
	name: 'systemRoleList',
	components: { EditRole },
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const editRoleRef = ref();
		const queryRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					roleName: '',
					roleStatus: '',
					pageNum: 1,
					pageSize: 10,
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			roleList();
		};
		const roleList = () => {
			getRoleList(state.tableData.param).then((res) => {
				state.tableData.data = res.data.list ?? [];
				state.tableData.total = res.data.total;
			});
		};
		// 打开新增角色弹窗
		const onOpenAddRole = () => {
			editRoleRef.value.openDialog();
		};
		// 打开修改角色弹窗
		const onOpenEditRole = (row: Object) => {
			editRoleRef.value.openDialog(toRaw(row));
		};
		// 删除角色
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除：“${row.name}”，是否继续`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteRole(row.id).then(() => {
						ElMessage.success('删除成功');
						proxy.$refs['editRoleRef'].resetMenuSession();
						roleList();
					});
				})
				.catch(() => {});
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			roleList();
		};
		// 格式化时间
		const formatter = (row: any, column: any, cellValue: any) => {
			return proxy.moment.unix(cellValue).format('YYYY-MM-DD HH:mm:ss');
		};
		return {
			queryRef,
			editRoleRef,
			resetQuery,
			onOpenAddRole,
			onOpenEditRole,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			roleList,
			formatter,
			...toRefs(state),
		};
	},
});
</script>
<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;

		.el-table {
			flex: 1;
		}
	}
}
</style>
